package com.zf.zfed.audit

import android.content.Context
import android.media.AudioManager
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.zf.zfed.audit.ui.VolumeSetItem
import com.zf.zfed.audit.ui.VolumeSlider

/**
 * 音量控制测试Activity
 * 用于演示VolumeSlider和VolumeSetItem的使用
 */
class VolumeTestActivity : AppCompatActivity() {
    
    private lateinit var volumeItem: VolumeSetItem
    private lateinit var volumeSlider: VolumeSlider
    private lateinit var tvVolumeInfo: TextView
    private lateinit var audioManager: AudioManager
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_volume_test)
        
        initViews()
        initAudioManager()
        setupVolumeControls()
    }
    
    private fun initViews() {
        volumeItem = findViewById(R.id.volume_item_1)
        volumeSlider = findViewById(R.id.volume_slider_media)
        tvVolumeInfo = findViewById(R.id.tv_volume_info)
    }
    
    private fun initAudioManager() {
        audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager
        
        // 获取当前音量并设置到控件
        val currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
        val maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)
        val currentProgress = (currentVolume.toFloat() / maxVolume.toFloat()) * 100f
        
        volumeItem.setVolume(currentProgress)
        volumeSlider.setProgress(currentProgress)
        updateVolumeInfo(currentProgress)
    }
    
    private fun setupVolumeControls() {
        // 设置VolumeSetItem的音量变化监听器
        volumeItem.setOnVolumeChangeListener(object : VolumeSlider.OnVolumeChangeListener {
            override fun onVolumeChanged(volume: Float) {
                setSystemVolume(volume)
                updateVolumeInfo(volume)
                // 同步另一个滑动条
                volumeSlider.setProgress(volume)
            }
        })
        
        // 设置VolumeSlider的音量变化监听器
        volumeSlider.setOnVolumeChangeListener(object : VolumeSlider.OnVolumeChangeListener {
            override fun onVolumeChanged(volume: Float) {
                setSystemVolume(volume)
                updateVolumeInfo(volume)
                // 同步另一个滑动条
                volumeItem.setVolume(volume)
            }
        })
    }
    
    private fun setSystemVolume(volume: Float) {
        val maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)
        val systemVolume = ((volume / 100f) * maxVolume).toInt()
        
        audioManager.setStreamVolume(
            AudioManager.STREAM_MUSIC,
            systemVolume,
            0 // 不显示系统音量UI，使用我们自己的
        )
    }
    
    private fun updateVolumeInfo(volume: Float) {
        val volumePercent = volume.toInt()
        tvVolumeInfo.text = "当前音量: $volumePercent%\n" +
                "系统音量: ${audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)}/" +
                "${audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)}"
    }
}